﻿using FastUtil.Config;
using FastUtil.Data;
using NVelocity;
using NVelocity.App;
using NVelocity.Runtime;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Base.Config
{
    public class TemplateUtil
    {
        private static VelocityEngine vltEngine;
        public static string CodeTempPath;

        private static void InitTemplateSetting()
        {
            CodeTempPath = AppConfigUtil.Configuration["Frame:GenerateCodeTemplatePath"];
            DirectoryInfo CodePath = new DirectoryInfo(CloudUtil.GetContentStaticFilePath() + CodeTempPath);
            if (!CodePath.Exists)
            {
                CodePath.Create();
            }
            vltEngine = new VelocityEngine();
            vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
            vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CloudUtil.GetContentPath() + "/" + "Template");
            vltEngine.Init();
        }

        public static string GeneratemeplateFile(string FileID, string TableName, string TemplateFileName, string CodeFileName, Dictionary<string, object> RenderDataDic)
        {
            InitTemplateSetting();
            DirectoryInfo CodePath = new DirectoryInfo(CloudUtil.GetContentStaticFilePath() + CodeTempPath + "/" + FileID);
            if (!CodePath.Exists)
            {
                CodePath.Create();
            }
            CodePath = new DirectoryInfo(CloudUtil.GetContentStaticFilePath() + CodeTempPath + "/" + FileID + "/" + TableName);
            if (!CodePath.Exists)
            {
                CodePath.Create();
            }
            VelocityContext vltContext = new VelocityContext();
            foreach (var item in RenderDataDic)
            {
                vltContext.Put(item.Key, item.Value);
            }
            Template vltTemplate = vltEngine.GetTemplate(TemplateFileName);
            System.IO.StringWriter vltWriter = new System.IO.StringWriter();
            vltTemplate.Merge(vltContext, vltWriter);
            string CodeContent = vltWriter.GetStringBuilder().ToString();
            string CodeFilePath = CloudUtil.GetContentStaticFilePath() + CodeTempPath + "/" + FileID + "/" + TableName + "/" + CodeFileName;
            //保存生成后的代码内容到文件
            FileUtil.SaveStringToFile(CodeFilePath, CodeContent);
            return CodeFilePath;
        }

        public static string GenerateTemplateContent(string TemplateFileName, Dictionary<string, object> RenderDataDic)
        {
            InitTemplateSetting();
            VelocityContext vltContext = new VelocityContext();
            foreach (var item in RenderDataDic)
            {
                vltContext.Put(item.Key, item.Value);
            }
            Template vltTemplate = vltEngine.GetTemplate(TemplateFileName);
            System.IO.StringWriter vltWriter = new System.IO.StringWriter();
            vltTemplate.Merge(vltContext, vltWriter);
            string CodeContent = vltWriter.GetStringBuilder().ToString();
            return CodeContent;
        }
    }
}
